﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Facebook.Schema;
using Facebook.Utility;
using SssDesktop.SSSReferences;
using GMap.NET.Properties;
using GMap.NET.MapProviders;
using GMap.NET.WindowsForms;
using GMap.NET;
using GMap.NET.Analytics;
using GMap.NET.CacheProviders;
using GMap.NET.Internals;
using GMap.NET.ObjectModel;
using GMap.NET.Projections;
using GMap.NET.WindowsForms.Markers;
using GMap.NET.WindowsForms.ToolTips;
using System.Collections;

namespace SssDesktop
{
    public partial class Form1 : Form
    {
        private SSSReferences.SupportShoppingSystemServiceClient proxy = null;

        private long numer = 0;
        private int permission = 0;
        private String[] shopName = null;
        private shop[] shops = null;
        private string[] shopsToSearch = null;
        private productShop[] productShops = null;
        private productShop[] mainProductShop = null;
        private shop[] listShops = null;
        private productShop[] listProducts = null;
        private GMapOverlay top;
        private GMapMarkerGoogleGreen m;
        private ArrayList products;
        private String city;
        private String country;
        private TextBox[] plusList = null;
        private double x = 0.0;
        private double y = 0.0;
        private int countMoveButtonNext;

        private List<GMapOverlay> overlays;
        private List<MapRoute> routes;
        private List<GMapRoute> roads;
        private GMapOverlay objects;
        private List<GMapMarker> shopMarkers;
        List<shop> choosenShops;


        /**
         * This method initialize the main form, setting map from Gmap library,
         * connection with Facebook and get methods from the webservice. 
         * */
        public Form1()
        {
            InitializeComponent();
            countMoveButtonNext = 0;
            GMaps.Instance.UseRouteCache = true;
            GMaps.Instance.UseGeocoderCache = true;
            GMaps.Instance.UsePlacemarkCache = true;

            products = new ArrayList();
            MainMap.MapProvider = GMapProviders.GoogleMap;
            MainMap.SetCurrentPositionByKeywords("Odense");
            MainMap.Manager.Mode = AccessMode.ServerAndCache;
            top = new GMapOverlay(MainMap, "top");

            facebookService = new Facebook.Winforms.Components.FacebookService();
            facebookService.ApplicationKey = "121376047970842";
            perms = new List<Enums.ExtendedPermissions>
                                                    {
                                                                        Enums.ExtendedPermissions.share_item,
                                                                        Enums.ExtendedPermissions.publish_stream,
                                                                        Enums.ExtendedPermissions.status_update
                                                                    };
            proxy = new SSSReferences.SupportShoppingSystemServiceClient();

        }


        /**
         * Function to run the function Facebook connect
         * and check the user if is a normal user,
         * admin user, block user or new user
         * */
        private void buttonLogin_Click(object sender, EventArgs e)
        {
            
            facebookService.ConnectToFacebook(perms);
            try
            {
                numer = facebookService.Api.Users.GetLoggedInUser();
            }
            catch (FacebookException)
            {
                numer = 0;
                
            }

            if (numer != 0)
            {
                permission = proxy.checkUser(numer);

                //pamietac o zrobieniu dla admina osobnego!
                if (permission == 1 || permission == 2)
                {
                    buttonLogin.Visible = false;
                    labelInfoNoLogin.Visible = false;
                    buttonLogout.Visible = true;
                    ManageUserButtons(true);

                    labelHello.Text = "Hello " + facebookService.Users.GetInfo().first_name +
                        " " + facebookService.Users.GetInfo().last_name;
                    pictureBoxFacebookImage.Image = facebookService.Users.GetInfo().picture_small;
                }
                if (permission == 4)
                {
                    buttonLogin.Visible = false;
                    labelInfoNoLogin.Visible = false;
                    buttonLogout.Visible = true;
                    ManageUserButtons(true);

                    labelHello.Text = "Hello " + facebookService.Users.GetInfo().first_name +
                        " " + facebookService.Users.GetInfo().last_name + " in the first time in application";
                    pictureBoxFacebookImage.Image = facebookService.Users.GetInfo().picture_small;

                }
                if (permission == 0)
                {
                    buttonLogin.Visible = false;
                    labelInfoNoLogin.Visible = false;
                    buttonLogout.Visible = true;
                    ManageUserButtons(false);

                    labelHello.Text = "Hello " + facebookService.Users.GetInfo().first_name +
                        " " + facebookService.Users.GetInfo().last_name + " you are blocked in system";
                    pictureBoxFacebookImage.Image = facebookService.Users.GetInfo().picture_small;

                }
                if (permission == 2)
                {
                    buttonLogin.Visible = false;
                    labelInfoNoLogin.Visible = false;
                    buttonLogout.Visible = true;
                    ManageUserButtons(false);

                    labelHello.Text = "Hello " + facebookService.Users.GetInfo().first_name +
                        " " + facebookService.Users.GetInfo().last_name + " you are admin in the system!";
                    pictureBoxFacebookImage.Image = facebookService.Users.GetInfo().picture_small;

                }
            }

         }

        /**
         * Function to manage (hide and visible user buttons
         * @param option
         * 
         */
        private void ManageUserButtons(bool option)
        {

            buttonLatestProducts.Enabled = option;
            buttonAddNewProduct.Enabled = option;
            buttonSearchForProducts.Enabled = option;
            buttonSearchForShops.Enabled = option;
            //buttonFindShop.Enabled = option;
            buttonShopingList.Enabled = option;
        }
        /**
         * Function to go to a form where product can added
         * */
        private void buttonAddNewProduct_Click(object sender, EventArgs e)
        {
            panelMap.Visible = false;
            groupBoxAddNewProduct.Visible = true;
            groupBoxLatestProduct.Visible = false;
            panelShopinglist.Visible = false;
            panelSearchShop.Visible = false;
            panelSearchProduct.Visible = false;
            
            
        }


        /**
         * Function to choice shop to add product
         * */

        private void comboBoxShopName_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxShopStreet.Items.Clear();
            EnableItemsAddNewProduct(false);
            comboBoxShopStreet.Enabled = true;

            String tmp = shopName.ElementAt(comboBoxShopName.SelectedIndex);
            shops = proxy.getShopByName(tmp);
            foreach (var item in shops)
            {
                comboBoxShopStreet.Items.Add(item.street.ToString() + " " + item.number.ToString());
            }
            comboBoxShopStreet.Refresh();
        }

        /**
        * Function to choice street to add product
        * */
        private void comboBoxShopStreet_SelectedIndexChanged(object sender, EventArgs e)
        {
            EnableItemsAddNewProduct(true);
        }

        /**
        * Function visible items in a form add product
         * @param option
        * */
        private void EnableItemsAddNewProduct(bool option)
        {
            textBoxBrand.Enabled = option;
            textBoxInfo.Enabled = option;
            textBoxHowPacked.Enabled = option;
            textBoxNameProduct.Enabled = option;
            numericUpDownPrice.Enabled = option;
            buttonAddProduct.Enabled = option;
        }

        /**
       * Function added new product to the system
       * */
        private void buttonAddProduct_Click(object sender, EventArgs e)
        {
            if (isEmptyFieldsAddNewProduct() == true)
            {
                MessageBox.Show("'Name' and 'How packed' can not be empty. 'Price' has to be greater than 0.", "Empty fields",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                product newProduct = new product();
                newProduct.price = double.Parse(numericUpDownPrice.Value.ToString());
                newProduct.name = textBoxNameProduct.Text.ToLower();
                newProduct.howPacket = textBoxHowPacked.Text;
                newProduct.brand = textBoxBrand.Text;
                newProduct.info = textBoxInfo.Text;
                shop tmpShop = shops.ElementAt(comboBoxShopStreet.SelectedIndex);
                int added = proxy.addNewProduct(numer, newProduct, tmpShop);
                if (added == 0)
                {
                    MessageBox.Show("Product already exist", "Important Message");  
                }
                if (added == 1)
                {
                    MessageBox.Show("Product add correctly", "Important Message");  
                    EnableItemsAddNewProduct(false);
                }
                buttonShare.Visible = true;
            }
        }


        /**
       * Function to check the fields are not empty
         * @return true or false
       * */
        private bool isEmptyFieldsAddNewProduct()
        {
            if (textBoxNameProduct.TextLength == 0 || textBoxHowPacked.TextLength == 0
                || numericUpDownPrice.Value.ToString() == "0" || numericUpDownPrice.Value.ToString() == "0,00" || textBoxBrand.TextLength==0 || textBoxInfo.TextLength==0)
            {
                return true;
            }
            return false;
        }

        /**
       * Function to share information about added product in facebook.
       * */
        private void buttonShare_Click(object sender, EventArgs e)
        {
            string street = shopName.ElementAt(comboBoxShopName.SelectedIndex) +
                ", " + shops.ElementAt(comboBoxShopStreet.SelectedIndex).street +
                " " + shops.ElementAt(comboBoxShopStreet.SelectedIndex).number.ToString();
            facebookService.Api.Status.Set("added new product:\nName: " + textBoxNameProduct.Text + "\nPrice: " + numericUpDownPrice.Value.ToString() + " DKK" + "\nInfo: "
                + textBoxInfo.Text + "\nHow many: " + textBoxHowPacked.Text + "\nBrand: " + textBoxBrand.Text
                + "\nShop: " + street);
            
            buttonShare.Visible = false;
        }

        /**
       * Function to show Products Shops items.
         * @param i
       * */
        private void showProductsShops(int i)
        {


            if ((i + 1 + 1) > productShops.Count())
            {
                textBoxNumber1.Text = "";
                textBoxName1.Text = "";
                textBoxPrice1.Text = "";
                textBoxHP1.Text = "";
                textBoxBrand1.Text = "";
                textBoxInfo1.Text = "";
                textBoxDate1.Text = "";
                textBoxShop1.Text = "";
                textBoxStreet1.Text = "";
                textBoxNr1.Text = "";
                textBoxP1.Text = "";
            }
            else
            {
                textBoxNumber1.Text = (i + 1).ToString();
                textBoxName1.Text = productShops.ElementAt(i).product.name.ToString();
                textBoxPrice1.Text = productShops.ElementAt(i).product.price.ToString();
                textBoxHP1.Text = productShops.ElementAt(i).product.howPacket.ToString();
                textBoxBrand1.Text = productShops.ElementAt(i).product.brand.ToString();
                textBoxInfo1.Text = productShops.ElementAt(i).product.info.ToString();
                textBoxDate1.Text = productShops.ElementAt(i).product.data.ToString();
                textBoxShop1.Text = productShops.ElementAt(i).shop.name.ToString();
                textBoxStreet1.Text = productShops.ElementAt(i).shop.street.ToString();
                textBoxNr1.Text = productShops.ElementAt(i).shop.number.ToString();
                textBoxP1.Text = productShops.ElementAt(i).product.plus.ToString();
            }
            if ((i + 2 + 1) > productShops.Count())
            {
                textBoxNumber2.Text = "";
                textBoxName2.Text = "";
                textBoxPrice2.Text = "";
                textBoxHP2.Text = "";
                textBoxBrand2.Text = "";
                textBoxInfo2.Text = "";
                textBoxDate2.Text = "";
                textBoxShop2.Text = "";
                textBoxStreet2.Text = "";
                textBoxNr2.Text = "";
                textBoxP2.Text = "";
            }
            else
            {
                textBoxNumber2.Text = (i + 2).ToString();
                textBoxName2.Text = productShops.ElementAt(i + 1).product.name.ToString();
                textBoxPrice2.Text = productShops.ElementAt(i + 1).product.price.ToString();
                textBoxHP2.Text = productShops.ElementAt(i + 1).product.howPacket.ToString();
                textBoxBrand2.Text = productShops.ElementAt(i + 1).product.brand.ToString();
                textBoxInfo2.Text = productShops.ElementAt(i + 1).product.info.ToString();
                textBoxDate2.Text = productShops.ElementAt(i + 1).product.data.ToString();
                textBoxShop2.Text = productShops.ElementAt(i + 1).shop.name.ToString();
                textBoxStreet2.Text = productShops.ElementAt(i + 1).shop.street.ToString();
                textBoxNr2.Text = productShops.ElementAt(i + 1).shop.number.ToString();
                textBoxP2.Text = productShops.ElementAt(i + 1).product.plus.ToString();
            }
            if ((i + 3 + 1) > productShops.Count())
            {
                textBoxNumber3.Text = "";
                textBoxName3.Text = "";
                textBoxPrice3.Text = "";
                textBoxHP3.Text = "";
                textBoxBrand3.Text = "";
                textBoxInfo3.Text = "";
                textBoxDate3.Text = "";
                textBoxShop3.Text = "";
                textBoxStreet3.Text = "";
                textBoxNr3.Text = "";
                textBoxP3.Text = "";
            }
            else
            {

                textBoxNumber3.Text = (i + 3).ToString();
                textBoxName3.Text = productShops.ElementAt(i + 2).product.name.ToString();
                textBoxPrice3.Text = productShops.ElementAt(i + 2).product.price.ToString();
                textBoxHP3.Text = productShops.ElementAt(i + 2).product.howPacket.ToString();
                textBoxBrand3.Text = productShops.ElementAt(i + 2).product.brand.ToString();
                textBoxInfo3.Text = productShops.ElementAt(i + 2).product.info.ToString();
                textBoxDate3.Text = productShops.ElementAt(i + 2).product.data.ToString();
                textBoxShop3.Text = productShops.ElementAt(i + 2).shop.name.ToString();
                textBoxStreet3.Text = productShops.ElementAt(i + 2).shop.street.ToString();
                textBoxNr3.Text = productShops.ElementAt(i + 2).shop.number.ToString();
                textBoxP3.Text = productShops.ElementAt(i + 2).product.plus.ToString();
            }

            if ((i + 4 + 1) > productShops.Count())
            {
                textBoxNumber4.Text = "";
                textBoxName4.Text = "";
                textBoxPrice4.Text = "";
                textBoxHP4.Text = "";
                textBoxBrand4.Text = "";
                textBoxInfo4.Text = "";
                textBoxDate4.Text = "";
                textBoxShop4.Text = "";
                textBoxStreet4.Text = "";
                textBoxNr4.Text = "";
                textBoxP4.Text = "";
            }
            else
            {
                textBoxNumber4.Text = (i + 4).ToString();
                textBoxName4.Text = productShops.ElementAt(i + 3).product.name.ToString();
                textBoxPrice4.Text = productShops.ElementAt(i + 3).product.price.ToString();
                textBoxHP4.Text = productShops.ElementAt(i + 3).product.howPacket.ToString();
                textBoxBrand4.Text = productShops.ElementAt(i + 3).product.brand.ToString();
                textBoxInfo4.Text = productShops.ElementAt(i + 3).product.info.ToString();
                textBoxDate4.Text = productShops.ElementAt(i + 3).product.data.ToString();
                textBoxShop4.Text = productShops.ElementAt(i + 3).shop.name.ToString();
                textBoxStreet4.Text = productShops.ElementAt(i + 3).shop.street.ToString();
                textBoxNr4.Text = productShops.ElementAt(i + 3).shop.number.ToString();
                textBoxP4.Text = productShops.ElementAt(i + 3).product.plus.ToString();
            }
            if ((i + 5 + 1) > productShops.Count())
            {
                textBoxNumber5.Text = "";
                textBoxName5.Text = "";
                textBoxPrice5.Text = "";
                textBoxHP5.Text = "";
                textBoxBrand5.Text = "";
                textBoxInfo5.Text = "";
                textBoxDate5.Text = "";
                textBoxShop5.Text = "";
                textBoxStreet5.Text = "";
                textBoxNr5.Text = "";
                textBoxP5.Text = "";
            }
            else
            {
                textBoxNumber5.Text = (i + 5).ToString();
                textBoxName5.Text = productShops.ElementAt(i + 4).product.name.ToString();
                textBoxPrice5.Text = productShops.ElementAt(i + 4).product.price.ToString();
                textBoxHP5.Text = productShops.ElementAt(i + 4).product.howPacket.ToString();
                textBoxBrand5.Text = productShops.ElementAt(i + 4).product.brand.ToString();
                textBoxInfo5.Text = productShops.ElementAt(i + 4).product.info.ToString();
                textBoxDate5.Text = productShops.ElementAt(i + 4).product.data.ToString();
                textBoxShop5.Text = productShops.ElementAt(i + 4).shop.name.ToString();
                textBoxStreet5.Text = productShops.ElementAt(i + 4).shop.street.ToString();
                textBoxNr5.Text = productShops.ElementAt(i + 4).shop.number.ToString();
                textBoxP5.Text = productShops.ElementAt(i + 4).product.plus.ToString();
            }

            if ((i + 6 + 1) > productShops.Count())
            {
                textBoxNumber6.Text = "";
                textBoxName6.Text = "";
                textBoxPrice6.Text = "";
                textBoxHP6.Text = "";
                textBoxBrand6.Text = "";
                textBoxInfo6.Text = "";
                textBoxDate6.Text = "";
                textBoxShop6.Text = "";
                textBoxStreet6.Text = "";
                textBoxNr6.Text = "";
                textBoxP6.Text = "";


            }
            else
            {
                textBoxNumber6.Text = (i + 6).ToString();
                textBoxName6.Text = productShops.ElementAt(i + 5).product.name.ToString();
                textBoxPrice6.Text = productShops.ElementAt(i + 5).product.price.ToString();
                textBoxHP6.Text = productShops.ElementAt(i + 5).product.howPacket.ToString();
                textBoxBrand6.Text = productShops.ElementAt(i + 5).product.brand.ToString();
                textBoxInfo6.Text = productShops.ElementAt(i + 5).product.info.ToString();
                textBoxDate6.Text = productShops.ElementAt(i + 5).product.data.ToString();
                textBoxShop6.Text = productShops.ElementAt(i + 5).shop.name.ToString();
                textBoxStreet6.Text = productShops.ElementAt(i + 5).shop.street.ToString();
                textBoxNr6.Text = productShops.ElementAt(i + 5).shop.number.ToString();
                textBoxP6.Text = productShops.ElementAt(i + 5).product.plus.ToString();
            }

            if ((i + 7 + 1) > productShops.Count())
            {
                textBoxNumber7.Text = "";
                textBoxName7.Text = "";
                textBoxPrice7.Text = "";
                textBoxHP7.Text = "";
                textBoxBrand7.Text = "";
                textBoxInfo7.Text = "";
                textBoxDate7.Text = "";
                textBoxShop7.Text = "";
                textBoxStreet7.Text = "";
                textBoxNr7.Text = "";
                textBoxP7.Text = "";


            }
            else
            {

                textBoxNumber7.Text = (i + 7).ToString();
                textBoxName7.Text = productShops.ElementAt(i + 6).product.name.ToString();
                textBoxPrice7.Text = productShops.ElementAt(i + 6).product.price.ToString();
                textBoxHP7.Text = productShops.ElementAt(i + 6).product.howPacket.ToString();
                textBoxBrand7.Text = productShops.ElementAt(i + 6).product.brand.ToString();
                textBoxInfo7.Text = productShops.ElementAt(i + 6).product.info.ToString();
                textBoxDate7.Text = productShops.ElementAt(i + 6).product.data.ToString();
                textBoxShop7.Text = productShops.ElementAt(i + 6).shop.name.ToString();
                textBoxShop7.Text = productShops.ElementAt(i + 6).shop.name.ToString();
                textBoxStreet7.Text = productShops.ElementAt(i + 6).shop.street.ToString();
                textBoxNr7.Text = productShops.ElementAt(i + 6).shop.number.ToString();
                textBoxP7.Text = productShops.ElementAt(i + 6).product.plus.ToString();
            }

            if ((i + 8 + 1) > productShops.Count())
            {
                textBoxNumber8.Text = "";
                textBoxName8.Text = "";
                textBoxPrice8.Text = "";
                textBoxHP8.Text = "";
                textBoxBrand8.Text = "";
                textBoxInfo8.Text = "";
                textBoxDate8.Text = "";
                textBoxShop8.Text = "";
                textBoxStreet8.Text = "";
                textBoxNr8.Text = "";
                textBoxP8.Text = "";


            }
            else
            {

                textBoxNumber8.Text = (i + 8).ToString();
                textBoxName8.Text = productShops.ElementAt(i + 7).product.name.ToString();
                textBoxPrice8.Text = productShops.ElementAt(i + 7).product.price.ToString();
                textBoxHP8.Text = productShops.ElementAt(i + 7).product.howPacket.ToString();
                textBoxBrand8.Text = productShops.ElementAt(i + 7).product.brand.ToString();
                textBoxInfo8.Text = productShops.ElementAt(i + 7).product.info.ToString();
                textBoxDate8.Text = productShops.ElementAt(i + 7).product.data.ToString();
                textBoxShop8.Text = productShops.ElementAt(i + 7).shop.name.ToString();
                textBoxStreet8.Text = productShops.ElementAt(i + 7).shop.street.ToString();
                textBoxNr8.Text = productShops.ElementAt(i + 7).shop.number.ToString();
                textBoxP8.Text = productShops.ElementAt(i + 7).product.plus.ToString();
            }
            if ((i + 9 + 1) > productShops.Count())
            {
                textBoxNumber9.Text = "";
                textBoxName9.Text = "";
                textBoxPrice9.Text = "";
                textBoxHP9.Text = "";
                textBoxBrand9.Text = "";
                textBoxInfo9.Text = "";
                textBoxDate9.Text = "";
                textBoxShop9.Text = "";
                textBoxStreet9.Text = "";
                textBoxNr9.Text = "";
                textBoxP9.Text = "";

            }
            else
            {
                textBoxNumber9.Text = (i + 9).ToString();
                textBoxName9.Text = productShops.ElementAt(i + 8).product.name.ToString();
                textBoxPrice9.Text = productShops.ElementAt(i + 8).product.price.ToString();
                textBoxHP9.Text = productShops.ElementAt(i + 8).product.howPacket.ToString();
                textBoxBrand9.Text = productShops.ElementAt(i + 8).product.brand.ToString();
                textBoxInfo9.Text = productShops.ElementAt(i + 8).product.info.ToString();
                textBoxDate9.Text = productShops.ElementAt(i + 8).product.data.ToString();
                textBoxShop9.Text = productShops.ElementAt(i + 8).shop.name.ToString();
                textBoxStreet9.Text = productShops.ElementAt(i + 8).shop.street.ToString();
                textBoxNr9.Text = productShops.ElementAt(i + 8).shop.number.ToString();
                textBoxP9.Text = productShops.ElementAt(i + 8).product.plus.ToString();
            }
            if ((i + 10) > productShops.Count())
            {
                textBoxNumber10.Text = "";
                textBoxName10.Text = "";
                textBoxPrice10.Text = "";
                textBoxHP10.Text = "";
                textBoxBrand10.Text = "";
                textBoxInfo10.Text = "";
                textBoxDate10.Text = "";
                textBoxShop10.Text = "";
                textBoxStreet10.Text = "";
                textBoxNr10.Text = "";
                textBoxP10.Text = "";

            }
            else
            {
                textBoxNumber10.Text = (i + 10).ToString();
                textBoxName10.Text = productShops.ElementAt(i + 9).product.name.ToString();
                textBoxPrice10.Text = productShops.ElementAt(i + 9).product.price.ToString();
                textBoxHP10.Text = productShops.ElementAt(i + 9).product.howPacket.ToString();
                textBoxBrand10.Text = productShops.ElementAt(i + 9).product.brand.ToString();
                textBoxInfo10.Text = productShops.ElementAt(i + 9).product.info.ToString();
                textBoxDate10.Text = productShops.ElementAt(i + 9).product.data.ToString();
                textBoxShop10.Text = productShops.ElementAt(i + 9).shop.name.ToString();
                textBoxStreet10.Text = productShops.ElementAt(i + 9).shop.street.ToString();
                textBoxNr10.Text = productShops.ElementAt(i + 9).shop.number.ToString();
                textBoxP10.Text = productShops.ElementAt(i + 9).product.plus.ToString();
            }







        }

        /**
       * Function to define which product was cliced
       * */
        private void buttonNumberOfProduct_Click(object sender, EventArgs e)
        {
            productShops = proxy.getRightAmountProductShop(int.Parse(numericUpDownNumerOfProduct.Value.ToString()));
            int numberP = productShops.Count();
            if (numberP > 10)
            {
                buttonNext.Enabled = true;
            }

            showProductsShops(0);
            countMoveButtonNext = 0;
            buttonBack.Enabled = false;
        }
        
        
        /**
       * Function to show a form with latest product added.
       * */

        private void buttonLatestProducts_Click(object sender, EventArgs e)
        {
            panelMap.Visible = false;
            groupBoxAddNewProduct.Visible = false;
            groupBoxLatestProduct.Visible = true;
            panelShopinglist.Visible = false;
            panelSearchShop.Visible = false;
            panelSearchProduct.Visible = false;
            
        }


        /**
      * Function to show a form with to find shop
      * */
        private void buttonFindShop_Click(object sender, EventArgs e)
        {
            if (comboBoxTransport.SelectedIndex >= 0 && comboBoxTransport.SelectedIndex <= 2)
            {
                panelMap.Visible = true;
                groupBoxAddNewProduct.Visible = false;
                groupBoxLatestProduct.Visible = false;
                panelShopinglist.Visible = false;
                panelSearchShop.Visible = false;
                panelSearchProduct.Visible = false;
            }
            else
            {
                MessageBox.Show("You have to check transport!");
            }
            
            
        }

        /**
      * Function to show a form with latest product added.
      * */

        private void buttonGo_Click(object sender, EventArgs e)
        {
            
            int x;
            x = textBoxCityName.Text.IndexOf(",");
            //country = textBoxCityName.Text.Substring(0, x);
            //city = textBoxCityName.Text.Substring(x + 2, textBoxCityName.Text.Length - x - 2);
            country = "Denmark";
            city = "Odense";
            MainMap.SetCurrentPositionByKeywords(textBoxCityName.Text);
            MainMap.Visible = true;
        }

        /**
      * Function to return which kind of transport user choice.
         * @param transport
      * */
        private int returnTransport(ComboBox transport)
        {
            if (String.Compare(transport.SelectedItem.ToString(), "walk", true) == 0)
            {
                return 0;

            }
            if (String.Compare(transport.SelectedItem.ToString(), "bike",true) == 0)
            {
                return 1;
            }
            if (String.Compare(transport.SelectedItem.ToString(), "car", true) == 0)
            {
                return 2;
            }
            else
            {
                return -1;
            }
        }

        /**
      * Function to show on map where user can go to buy products from list shop, from the algorytm
         * to search shop form the webservice
      * */
        private void buttonSearchShop_Click(object sender, EventArgs e)
        {

            if (x == 0.0 && y == 0.0)
            {
                MessageBox.Show("You have to check point on map where you are!", "Important Message");
            }
            else
            {
                if (roads != null)
                {
                    for (int i = 0; i < choosenShops.Count(); i++)
                    {
                        overlays.ElementAt(i).Routes.Remove(roads.ElementAt(i));
                        objects.Markers.Remove(shopMarkers.ElementAt(i));
                    }
                }

                int hours = Convert.ToInt32(numericUpDownHours.Value.ToString());


                mainProductShop = proxy.searchProduct(x, y, products.ToArray(Type.GetType("System.String")) as string[], "Odense", "Denmark", returnTransport(comboBoxTransport), hours);
                if (mainProductShop.ElementAt(0).idproductShop == 0)
                {
                    if (mainProductShop.ElementAt(0).product.idproduct == 0)
                    {
                        MessageBox.Show("The is no products in database", "Important Message");
                    }
                    else
                    {
                        MessageBox.Show("Increase time to shop", "Important Message");
                    }

                }
                else
                {

                    productShop[] newMainProductShop = new productShop[mainProductShop.Count()];

                    choosenShops = new List<shop>();
                    choosenShops.Add(mainProductShop.ElementAt(0).shop);
                    bool isFound = false;
                    for (int i = 0; i < mainProductShop.Count(); i++)
                    {
                        isFound = false;
                        for (int j = 0; j < choosenShops.Count(); j++)
                        {
                            if (choosenShops.ElementAt(j).idshop == mainProductShop.ElementAt(i).shop.idshop)
                            {
                                isFound = true;
                                break;
                            }
                        }
                        if (isFound == false)
                        {
                            choosenShops.Add(mainProductShop.ElementAt(i).shop);
                        }
                    }


                    PointLatLng[] pkt = new PointLatLng[choosenShops.Count() + 1];
                    pkt[0] = new PointLatLng(x, y);
                    for (int i = 1; i < choosenShops.Count() + 1; i++)
                    {
                        pkt[i] = new PointLatLng(choosenShops.ElementAt(i - 1).x, choosenShops.ElementAt(i - 1).y);
                    }




                    overlays = new List<GMapOverlay>();
                    for (int i = 0; i < choosenShops.Count(); i++)
                    {
                        overlays.Add(new GMapOverlay(MainMap, "routes" + (i + 1).ToString()));
                    }
                    objects = new GMapOverlay(MainMap, "objects");

                    routes = new List<MapRoute>();
                    for (int i = 0; i < choosenShops.Count(); i++)
                    {
                        routes.Add(GMapProviders.GoogleMap.GetRouteBetweenPoints(pkt[i], pkt[i + 1], false, true, (int)MainMap.Zoom));
                    }

                    roads = new List<GMapRoute>();
                    for (int i = 0; i < choosenShops.Count(); i++)
                    {
                        if (i == 0)
                        {
                            roads.Add(new GMapRoute(routes.ElementAt(i).Points, "My place to " + choosenShops.ElementAt(i).name));
                        }
                        else
                        {
                            roads.Add(new GMapRoute(routes.ElementAt(i).Points, choosenShops.ElementAt(i - 1).name + " to " + choosenShops.ElementAt(i).name));
                        }
                    }

                    for (int i = 0; i < choosenShops.Count(); i++)
                    {
                        overlays.ElementAt(i).Routes.Add(roads.ElementAt(i));
                    }

                    shopMarkers = new List<GMapMarker>();
                    for (int i = 0; i < choosenShops.Count(); i++)
                    {
                        shopMarkers.Add(new GMapMarkerGoogleRed(pkt[i + 1]));
                    }

                    List<string> productsName = new List<string>();
                    for (int i = 0; i < choosenShops.Count(); i++)
                    {
                        productsName.Add("");
                    }

                    for (int i = 0; i < mainProductShop.Count(); i++)
                    {
                        for (int j = 0; j < choosenShops.Count(); j++)
                        {
                            if (mainProductShop.ElementAt(i).shop.idshop == choosenShops.ElementAt(j).idshop)
                            {
                                productsName[j] = productsName[j] + " \n" + mainProductShop.ElementAt(i).product.name +
                                " " + mainProductShop.ElementAt(i).product.price.ToString() + " DKK";
                            }
                        }
                    }

                    for (int i = 0; i < choosenShops.Count(); i++)
                    {
                        shopMarkers.ElementAt(i).ToolTipText = choosenShops.ElementAt(i).name + productsName.ElementAt(i);
                        objects.Markers.Add(shopMarkers.ElementAt(i));
                        MainMap.Overlays.Add(overlays.ElementAt(i));
                    }
                    MainMap.Overlays.Add(objects);
                    MainMap.Refresh();
                }
            }
        }


        /**
      * Function to show a form to add shopping list
      * */
        private void buttonShopingList_Click(object sender, EventArgs e)
        {
            panelShopinglist.Visible = true;
            panelMap.Visible = false;
            groupBoxAddNewProduct.Visible = false;
            groupBoxLatestProduct.Visible = false;
            panelSearchShop.Visible = false;
            panelSearchProduct.Visible = false;
            

        }


        /**
      * Function to add product to shoping list.
      * */
        private void buttonAddtoShopingList_Click(object sender, EventArgs e)
        {
            listViewShopList.Items.Add(textBoxProductAdd.Text);
            products.Add(textBoxProductAdd.Text.ToLower());
            textBoxProductAdd.Text = "";
            buttonFindShop.Enabled = true;
        }

        /**
      * Function to remove product from the shoping list
      * */
        private void buttonRemoveFromShopingList_Click(object sender, EventArgs e)
        {

            if (listViewShopList.Items.Count == 0)
            {


            }
            if (listViewShopList.Items.Count == 1)
            {
                listViewShopList.Items.RemoveAt(0);
                products.RemoveAt(0);
                buttonFindShop.Enabled = false;
            }
            if (listViewShopList.Items.Count > 1)
            {
                int tmp = listViewShopList.Items.Count;
                listViewShopList.Items.RemoveAt(tmp - 1);
                products.RemoveAt(tmp - 1);
            }
            
        }

        /**
      * Function to show next product in the latetes products form.
      * */
        private void buttonNext_Click(object sender, EventArgs e)
        {
            countMoveButtonNext++;
            showProductsShops(10 * countMoveButtonNext);

            if (productShops.Count() < countMoveButtonNext* 10 +11)
            {
                buttonNext.Enabled = false;
            }
            //if(productShops.Count() < 
            buttonBack.Enabled = true;
        }

        /**
        * Function to show back product in the latetes products form.
        * */
        private void button1_Click(object sender, EventArgs e)
        {
            countMoveButtonNext--;
            showProductsShops(10 * countMoveButtonNext);
            if (countMoveButtonNext == 0)
            {
                buttonBack.Enabled = false;
                buttonNext.Enabled = true;
            }
        }

        /**
        * Function to plus product rating
        * */
        private void buttonPlus1_Click(object sender, EventArgs e)
        {
            Button test = (Button)sender;
            char tmp = test.Name.Last();

            int which = Convert.ToInt32(new string(tmp, 1));
            int testRes = proxy.plusProduct(Convert.ToInt32(productShops.ElementAt(which + countMoveButtonNext * 10).product.idproduct.ToString()), numer,1);
            if (testRes == 0)
            {
                MessageBox.Show("You cannot plus once again product.", "Important Message");

            }
            if (testRes == 1)
            {
                MessageBox.Show("It's ok", "Important Message");
                addListTextBox();
                int plus = 0;
                plus = Convert.ToInt32(plusList[which].Text) + 1;
                plusList[which].Text = plus.ToString();

            }
            

        }

        private void addListTextBox()
        {
            plusList = new TextBox[10];

            plusList[0] = textBoxP1;
            plusList[1] = textBoxP2;
            plusList[2] = textBoxP3;
            plusList[3] = textBoxP4;
            plusList[4] = textBoxP5;
            plusList[5] = textBoxP6;
            plusList[6] = textBoxP7;
            plusList[7] = textBoxP8;
            plusList[8] = textBoxP9;
            plusList[9] = textBoxP10;

        }

        /**
       * Function to show the form with Search for Shop
       * */
        private void buttonSearchForShops_Click(object sender, EventArgs e)
        {
            panelMap.Visible = false;
            groupBoxAddNewProduct.Visible = false;
            groupBoxLatestProduct.Visible = false;
            panelShopinglist.Visible = false;
            panelSearchShop.Visible = true;
            panelSearchProduct.Visible = false;
            
        }

        /**
       * Function to search city on the map
       * */
        private void buttonSearchCity_Click(object sender, EventArgs e)
        {
            listViewSearchShop.Items.Clear();
            listShops = proxy.getShopByCity(textBoxShopCitySerach.Text);

            if (listShops == null)
            {
                MessageBox.Show("The is no shop in this city", "Important Message");
            }
            else
            {
                for (int i = 0; i < listShops.Count(); i++)
                {

                    ListViewItem x = new ListViewItem((i+1).ToString());
                    x.SubItems.Add(listShops.ElementAt(i).name);
                    x.SubItems.Add(listShops.ElementAt(i).street);
                    x.SubItems.Add(listShops.ElementAt(i).number.ToString());
                    x.SubItems.Add(listShops.ElementAt(i).city);
                    x.SubItems.Add(listShops.ElementAt(i).country);
                    listViewSearchShop.Items.Add(x);
                }
            }
        }


        /**
       * Function to show a form with search for product
       * */
        private void buttonSearchForProducts_Click(object sender, EventArgs e)
        {
            panelSearchProduct.Visible = true;
            panelMap.Visible = false;
            groupBoxAddNewProduct.Visible = false;
            groupBoxLatestProduct.Visible = false;
            panelShopinglist.Visible = false;
            panelSearchShop.Visible = false;
            
        }


        /**
         * Function to add search product
         * 
         * 
         */
        private void buttonSearchProduct_Click(object sender, EventArgs e)
        {
            
                addProductShopsToListView();
        }


        /**
       * Function to add product shops in list shop view
       * */
        private void addProductShopsToListView()
        {
            if (comboBoxNameStreetSearchProduct.SelectedItem.ToString() == "all" && textBoxSearchBrand.Text.Length != 0)
            {
                
                listProducts = proxy.getProductShopByNameProduct(textBoxSearchBrand.Text, -1);

            }
            if (comboBoxNameStreetSearchProduct.SelectedItem.ToString() == "all" && textBoxSearchBrand.Text=="")
            {
                
                listProducts = proxy.getProductShop();
            }
            if (textBoxSearchBrand.Text == "" && comboBoxNameStreetSearchProduct.SelectedItem.ToString() != "all")
            {
                
                int idshopu = shops.ElementAt(comboBoxNameStreetSearchProduct.SelectedIndex).idshop;
                listProducts = proxy.getProductShopByNameProduct("", idshopu);
            }
            if (textBoxSearchBrand.Text.Length !=0 && comboBoxNameStreetSearchProduct.SelectedItem.ToString() != "all")
            {
                ;
                int idshopu = shops.ElementAt(comboBoxNameStreetSearchProduct.SelectedIndex).idshop;
                listProducts = proxy.getProductShopByNameProduct(textBoxSearchBrand.Text, idshopu);
                
            }
            

                //int idshopu = shops.ElementAt(comboBoxNameStreetSearchProduct.SelectedIndex).idshop;
                listViewProducts.Items.Clear();
               // listProducts = proxy.getProductShopByNameProduct(textBoxSearchBrand.Text, idshopu);

                Button b = new Button();
                if (listProducts == null)
                {
                    MessageBox.Show("The is no product in the database", "Important Message");
                }
                else
                {

                    for (int i = 0; i < listProducts.Count(); i++)
                    {
                        int lp = i + 1;
                        ListViewItem x = new ListViewItem(lp.ToString());
                        x.SubItems.Add(listProducts.ElementAt(i).product.name);
                        x.SubItems.Add(listProducts.ElementAt(i).product.price.ToString());
                        x.SubItems.Add(listProducts.ElementAt(i).product.data);
                        x.SubItems.Add(listProducts.ElementAt(i).product.info);
                        x.SubItems.Add(listProducts.ElementAt(i).product.howPacket);
                        x.SubItems.Add(listProducts.ElementAt(i).product.brand);
                        x.SubItems.Add(listProducts.ElementAt(i).product.plus.ToString());
                        x.SubItems.Add(listProducts.ElementAt(i).shop.name);
                        x.SubItems.Add(listProducts.ElementAt(i).shop.street);
                        x.SubItems.Add(listProducts.ElementAt(i).shop.number.ToString());
                        x.SubItems.Add(listProducts.ElementAt(i).shop.city);
                        listViewProducts.Items.Add(x);

                    }
                }
            
        }

        /**
       * Function to add marker on map on position where the user is.
       * */
        private void MainMap_DoubleClick(object sender, MouseEventArgs e)
        {
            top.Markers.Remove(m);
            m = new GMapMarkerGoogleGreen(MainMap.FromLocalToLatLng(e.X, e.Y));
            m.ToolTipMode = MarkerTooltipMode.Always;
            m.ToolTipText = "You are here: " + m.Position.ToString();
            top.Markers.Add(m);
            x = m.Position.Lat;
            y = m.Position.Lng;
            MainMap.Overlays.Add(top);
        }

        /**
       * Function to plus product rating
       * */
        private void buttonPlus_Click(object sender, EventArgs e)
        {
            int item, idproduct;
            

            if (listViewProducts.SelectedItems.Count == 0)
            {
                MessageBox.Show("Please selected a product", "Important Message");    
            }
            else
            {

                for (int i = 0; i < listViewProducts.SelectedItems.Count; i++)
                {
                    item = listViewProducts.SelectedItems[i].Index;


                    idproduct = listProducts.ElementAt(item).product.idproduct;
                    int testRes = proxy.plusProduct(idproduct, numer,1);
                    if (testRes == 0)
                    {
                        MessageBox.Show("You cannot plus once again product.", "Important Message");

                    }
                    if (testRes == 1)
                    {
                        MessageBox.Show("It's ok", "Important Message");
                        addListTextBox();
                        addProductShopsToListView();

                    }
                }
           }
        }

        /**
       * Function to show on map user city
       * */
        private void buttonConfirmCity_Click(object sender, EventArgs e)
        {
            comboBoxShopName.Items.Clear();
            shopName = proxy.getShopName(textBoxConfirmCity.Text);
            if(shopName == null ){
                MessageBox.Show("There is no shop in this city or correct city", "Important Message");
            }
            else{

            foreach (var item in shopName)
            {
                comboBoxShopName.Items.Add(item);
            }
            comboBoxShopName.Refresh();
            comboBoxShopName.Enabled = true;
            }
        }

        /**
       * Function to clear shopping list
       * */
        private void buttonClear_Click(object sender, EventArgs e)
        {
            listViewShopList.Items.Clear();
            products.Clear();
            buttonFindShop.Enabled = false;
        }


        /**
       * Function to show a form with addshop
       * */
        private void buttonAddShop_Click(object sender, EventArgs e)
        {
            panelMap.Visible = false;
            groupBoxAddNewProduct.Visible = false;
            groupBoxLatestProduct.Visible = false;
            panelShopinglist.Visible = false;
            panelSearchShop.Visible = false;
            panelSearchProduct.Visible = false;
            
        }

        /**
       * Function to minus product rating
       * */
        private void buttonMinus1_Click(object sender, EventArgs e)
        {
            Button test = (Button)sender;
            char tmp = test.Name.Last();

            int which = Convert.ToInt32(new string(tmp, 1));
            int testRes = proxy.plusProduct(Convert.ToInt32(productShops.ElementAt(which + countMoveButtonNext * 10).product.idproduct.ToString()), numer,-1);
            if (testRes == 0)
            {
                MessageBox.Show("You cannot vote once again product.", "Important Message");

            }
            if (testRes == 1)
            {
                MessageBox.Show("It's ok", "Important Message");
                addListTextBox();
                int plus = 0;
                plus = Convert.ToInt32(plusList[which].Text) - 1;
                plusList[which].Text = plus.ToString();

            }
        }

        /**
       * Function to plus minus rating
       * */
        private void buttonMinus_Click(object sender, EventArgs e)
        {
            int item, idproduct;


            if (listViewProducts.SelectedItems.Count == 0)
            {
                MessageBox.Show("Please selected a product", "Important Message");
            }
            else
            {

                for (int i = 0; i < listViewProducts.SelectedItems.Count; i++)
                {
                    item = listViewProducts.SelectedItems[i].Index;


                    idproduct = listProducts.ElementAt(item).product.idproduct;
                    int testRes = proxy.plusProduct(idproduct, numer,-1);
                    if (testRes == 0)
                    {
                        MessageBox.Show("You cannot vote once again product.", "Important Message");

                    }
                    if (testRes == 1)
                    {
                        MessageBox.Show("It's ok", "Important Message");
                        addListTextBox();
                        addProductShopsToListView();

                    }
                }
            }
        }


        /**
       * Function to serach shop and put on the list
       * */
        private void button1_Click_1(object sender, EventArgs e)
        {

            shopsToSearch = proxy.getShopName(textBoxCitySearchProduct.Text);
            if (shopsToSearch == null)
            {
                MessageBox.Show("There is no shop in this city or correct city", "Important Message");
            }
            else
            {
                comboBoxNameShopSearchProduct.Items.Clear();
                
                foreach (var item in shopsToSearch)
                {
                    comboBoxNameShopSearchProduct.Items.Add(item);
                }
                comboBoxNameShopSearchProduct.Items.Add("all");
                comboBoxNameShopSearchProduct.Refresh();
                comboBoxNameShopSearchProduct.Enabled = true;
                
            }
        }



        /**
       * Function to show street names when comboBox of Name shop search product 
         * is changed
       * */
        private void comboBoxNameShopSearchProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            //shops = null;
            
            comboBoxNameStreetSearchProduct.Items.Remove(0);
            comboBoxNameStreetSearchProduct.Items.Clear();
            comboBoxNameStreetSearchProduct.Refresh();
           
            
            comboBoxNameStreetSearchProduct.Enabled = true;
            
            
            if (comboBoxNameShopSearchProduct.SelectedItem.ToString() == "all")
            {
               
                comboBoxNameStreetSearchProduct.Items.Clear();
                comboBoxNameStreetSearchProduct.Refresh();
                comboBoxNameStreetSearchProduct.Items.Add("all");
                comboBoxNameStreetSearchProduct.SelectedIndex = 0;
            }
            else
            {
                String tmp = shopsToSearch.ElementAt(comboBoxNameShopSearchProduct.SelectedIndex);
                shops = proxy.getShopByName(tmp);
                foreach (var item in shops)
                {
                    comboBoxNameStreetSearchProduct.Items.Add(item.street.ToString() + " " + item.number.ToString());
                }
                comboBoxNameStreetSearchProduct.Refresh();
                comboBoxNameStreetSearchProduct.Enabled = true;
                comboBoxNameStreetSearchProduct.SelectedIndex = 0;
            }
           

        }

        /**
       * Function to exit and logout the application
       **/
        private void buttonLogout_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /**
       * Function to save list on server
       * */
        private void buttonSaveList_Click(object sender, EventArgs e)
        {
            String saveLine = "";
            for (int i = 0; i < products.Count; i++)
            {
                saveLine += products[i] + ";";
            }
            int result = proxy.saveShoppingList(numer, saveLine);
        }

        /**
       * Function to check sho list on server
       * */
        private void buttonCheckList_Click(object sender, EventArgs e)
        {
            //products.Add(textBoxProductAdd.Text.ToLower());
            //listViewShopList.Items.Add(textBoxProductAdd.Text);

            String[] savedShoppingList = proxy.checkSavedShoppingList(numer);
            listViewShopList.Items.Clear();
            products.Clear();

            if (savedShoppingList[0].Equals("none"))
            {
                MessageBox.Show("You have no list on server saved.", "Important Message");
            }
            else 
            {
                buttonFindShop.Enabled = true;
                for (int i = 0; i < savedShoppingList.Count(); i++)
                {
                    products.Add(savedShoppingList[i]);
                    listViewShopList.Items.Add(savedShoppingList[i]);
                }
            }
        }

        

















    }
}
